package com.xzy.urp.listener;

import cn.hutool.core.bean.BeanUtil;
import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.xzy.urp.pojo.dto.ExcelStudentDTO;
import com.xzy.urp.pojo.dto.ExcelTeacherDTO;
import com.xzy.urp.pojo.vo.StudentInfoVO;
import com.xzy.urp.pojo.vo.TeacherInfoVO;
import com.xzy.urp.service.StudentInfoService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.dao.DuplicateKeyException;

@Slf4j
public class StudentExcelListener extends AnalysisEventListener<ExcelStudentDTO> {
    private final StudentInfoService service;

    public StudentExcelListener(StudentInfoService service) {
        this.service = service;
    }

    @Override
    public void invoke(ExcelStudentDTO data, AnalysisContext context) {

        StudentInfoVO studentInfoVO = new StudentInfoVO();
        BeanUtil.copyProperties(data,studentInfoVO);
        studentInfoVO.setUId(data.getId());

        try{
            service.addSInfo(studentInfoVO) ;
        }catch (DuplicateKeyException e){
            log.error("用户已存在"+studentInfoVO.getUId());
            //todo: 标记出现异常
        }
    }

    @Override
    public void doAfterAllAnalysed(AnalysisContext context) {
            // TODO: 判断是否出现异常并前端返回
    }
}
